package com.cmsky.open.food.controller;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.cmsky.open.base.manager.GoodsClassifyInfoManager;
import com.cmsky.open.base.service.MenuService;
import com.cmsky.open.base.vo.query.GoodsClassifyInfoQuery;
import com.cmsky.open.cms.model.CmsNodeInfo;
import com.cmsky.open.cms.model.GoodsNode;
import com.cmsky.open.cms.service.CmsNodeService;
import com.cmsky.open.core.base.BasePager;
import com.cmsky.open.core.util.JsonUtils;
import com.cmsky.open.core.util.RequestUtils;
import com.cmsky.open.food.enums.FoodState;
import com.cmsky.open.food.manager.FoodInfoManager;
import com.cmsky.open.food.model.FoodInfo;
import com.cmsky.open.food.util.NormalUtil;
import com.cmsky.open.food.vo.query.FoodInfoQuery;
import com.cmsky.open.shop.manager.ShopAreaInfoManager;
import com.cmsky.open.shop.manager.ShopInfoManager;
import com.cmsky.open.shop.model.ShopAreaInfo;
import com.cmsky.open.shop.model.ShopInfo;
import com.cmsky.open.shop.vo.query.ShopAreaInfoQuery;
import com.cmsky.open.user.service.UserLoginService;

@Controller("food.index")
@RequestMapping("/food/i")
public class IndexController {
	protected static final Logger logger = Logger.getLogger(NormalUtil.getLogName());
	private static final int DefaultPageNodeId = 4;
	protected static final Integer GOODS_FOOD_ROOT_ID = 7; //以后改成系统配置
	protected static final GoodsClassifyInfoQuery gQuery;
	
	static{
		gQuery = new GoodsClassifyInfoQuery();
		gQuery.setPid(GOODS_FOOD_ROOT_ID);
	}
	
	private MenuService menuService;
	private CmsNodeService cmsNodeService;
	private FoodInfoManager foodInfoManager;
	private ShopInfoManager shopInfoManager;
	private GoodsClassifyInfoManager goodsClassifyInfoManager;
	private ShopAreaInfoManager shopAreaInfoManager;
	private UserLoginService userLoginService;
	
	public void setShopAreaInfoManager(ShopAreaInfoManager shopAreaInfoManager) {
		this.shopAreaInfoManager = shopAreaInfoManager;
	}

	public void setGoodsClassifyInfoManager(
			GoodsClassifyInfoManager goodsClassifyInfoManager) {
		this.goodsClassifyInfoManager = goodsClassifyInfoManager;
	}

	public void setUserLoginService(UserLoginService userLoginService) {
		this.userLoginService = userLoginService;
	}
	
	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}

	public void setCmsNodeService(CmsNodeService cmsNodeService) {
		this.cmsNodeService = cmsNodeService;
	}
	
	public void setFoodInfoManager(FoodInfoManager foodInfoManager) {
		this.foodInfoManager = foodInfoManager;
	}

	public void setShopInfoManager(ShopInfoManager shopInfoManager) {
		this.shopInfoManager = shopInfoManager;
	}
	
	@ModelAttribute
	public void init(HttpServletRequest request,HttpServletResponse response,ModelMap model) {
		model.put("now", System.currentTimeMillis());
		model.put("headMenu", this.menuService.getNavigationMenu());
		model.put("goodsMenu", this.menuService.getGoodsClssfiyMenu());
		model.put("foodTypes", this.goodsClassifyInfoManager.findAllByQuery(gQuery));
		ShopAreaInfoQuery sQuery = new ShopAreaInfoQuery();
		sQuery.setAreaId(null);
		List<ShopAreaInfo> shopAreas = this.shopAreaInfoManager.findAllByQuery(sQuery);//待优化
		if(null!=shopAreas&&0<shopAreas.size()){
			Set<ShopInfo> shopers = new HashSet<ShopInfo>();
			for(ShopAreaInfo sa : shopAreas){
				shopers.add(shopInfoManager.getById(sa.getShopId()));
			}
			model.put("shopers", shopers);
		}
	}

	/**
	 * 进入nid栏目对应的快餐首页
	 */
	@RequestMapping("/index/{nid}")
	public ModelAndView indexNid(HttpServletRequest request,HttpServletResponse response,@PathVariable Integer nid){
		ModelAndView index = new ModelAndView("/food/index");
		if(nid <= 0){
			nid = DefaultPageNodeId;
			index.addObject("pageNid", nid);
		}
		Map<Integer,CmsNodeInfo> nodeMaps = this.cmsNodeService.findByPidForSid(nid);
		if(null!=nodeMaps){
			if(nodeMaps.containsKey(1)){//index 1 大广告图
				CmsNodeInfo bigGgPic = nodeMaps.get(1);
				index.addObject("bigGgPics", this.cmsNodeService.findByPid(bigGgPic.getId()));
			}
			if(nodeMaps.containsKey(2)){//快餐内容栏
				CmsNodeInfo foodCntentNode = nodeMaps.get(2);
				List<CmsNodeInfo> tempNodes = this.cmsNodeService.findByPid(foodCntentNode.getId());
				List<CmsNodeInfo> foodContentNodes = new LinkedList<CmsNodeInfo>();
				List<List<FoodInfo>> foodConntentItems = new LinkedList<List<FoodInfo>>();
				
				if(null!=tempNodes && 0<tempNodes.size()){
					for(CmsNodeInfo classify : tempNodes){
						List<CmsNodeInfo> items = this.cmsNodeService.findByPid(classify.getId());
						if(null!=items && 0<tempNodes.size()){
							List<FoodInfo> itemFoods = new LinkedList<FoodInfo>();
							for(CmsNodeInfo item : items){
								if(item instanceof GoodsNode){
									GoodsNode goodsNode = (GoodsNode)item;
									FoodInfo temp = foodInfoManager.getById(goodsNode.getGoodsId());
									temp.setShopInfo(shopInfoManager.getById(temp.getShopId()));
									itemFoods.add(temp);
								}
							}
							if(itemFoods.size()>0){
								foodConntentItems.add(itemFoods);
								foodContentNodes.add(classify);
							}
						}
					}
				}
				index.addObject("foodContentNodes", foodContentNodes);
				index.addObject("foodConntentItems", foodConntentItems);
			}
			
			if(nodeMaps.containsKey(3)){//快餐广告栏
				CmsNodeInfo foodGuanggaoNode = nodeMaps.get(3);
				List<CmsNodeInfo> tempNodes = this.cmsNodeService.findByPid(foodGuanggaoNode.getId());
				List<FoodInfo> foodGuanggaoItems = new LinkedList<FoodInfo>();
				
				if(null!=tempNodes && 0<tempNodes.size()){
					for(CmsNodeInfo item : tempNodes){
						if(item instanceof GoodsNode){
							GoodsNode goodsNode = (GoodsNode)item;
							FoodInfo temp = foodInfoManager.getById(goodsNode.getGoodsId());
							temp.setShopInfo(shopInfoManager.getById(temp.getShopId()));
							foodGuanggaoItems.add(temp);
						}
					}
				}
				index.addObject("foodGuanggaoNode", foodGuanggaoNode);
				index.addObject("foodGuanggaoItems", foodGuanggaoItems);
			}
		}
		return index;
	}
	
	/**
	 * 默认管理页
	 */
	@RequestMapping("/index")
	public String index(HttpServletRequest request,HttpServletResponse response){
		return "forward:/food/i/index/"+DefaultPageNodeId+".do";
	}
	
	/***
	 * 快餐-分类搜索项
	 * @param request
	 * @param response
	 * @param nid
	 * @return
	 */
	@RequestMapping("/search")
	public ModelAndView search(HttpServletRequest request,HttpServletResponse response){
		ModelAndView view = new ModelAndView("/food/moreFood");
		
		Integer nid = DefaultPageNodeId;//根据选择的地区，确定nid
		Integer areaId = null;//根据选择的地区，确定nid
		
		Map<Integer,CmsNodeInfo> nodeMaps = this.cmsNodeService.findByPidForSid(nid);
		if(null!=nodeMaps){
			if(nodeMaps.containsKey(3)){//快餐广告栏
				CmsNodeInfo foodGuanggaoNode = nodeMaps.get(3);
				List<CmsNodeInfo> tempNodes = this.cmsNodeService.findByPid(foodGuanggaoNode.getId());
				List<FoodInfo> foodGuanggaoItems = new LinkedList<FoodInfo>();
				
				if(null!=tempNodes && 0<tempNodes.size()){
					for(CmsNodeInfo item : tempNodes){
						if(item instanceof GoodsNode){
							GoodsNode goodsNode = (GoodsNode)item;
							FoodInfo temp = foodInfoManager.getById(goodsNode.getGoodsId());
							temp.setShopInfo(shopInfoManager.getById(temp.getShopId()));
							foodGuanggaoItems.add(temp);
						}
					}
				}
				view.addObject("foodGuanggaoNode", foodGuanggaoNode);
				view.addObject("foodGuanggaoItems", foodGuanggaoItems);
			}
		}
		
		Integer goodClassfyId = RequestUtils.getInt(request, "gid", null);
		FoodInfoQuery query = new FoodInfoQuery();
		query.setState(FoodState.Uped.getValue());
		query.setAreaId(areaId);
		query.setShopId(RequestUtils.getInt(request, "sid", null));
		query.setName(RequestUtils.getString(request, "fname", null)); 
		query.setType(goodClassfyId);
		query.setPagesize(16);
		BasePager<FoodInfo> pagers = this.foodInfoManager.findPage(query);
		
		view.addObject("moreClassfiy", goodsClassifyInfoManager.getById(goodClassfyId));
		List<FoodInfo> moreFoods = pagers.getRows();
		if(null!=moreFoods&&0<moreFoods.size()){
			for(FoodInfo f : moreFoods){
				f.setShopInfo(this.shopInfoManager.getById(f.getId()));
			}
	 		view.addObject("moreFoods", moreFoods);
		}
		
		return view;
	}
	
	/***
	 * 快餐-分类搜索项
	 * @param request
	 * @param response
	 * @param nid
	 * @return
	 */
	@RequestMapping("/ajaxSearch")
	public ModelAndView ajaxSearch(HttpServletRequest request,HttpServletResponse response){
		ModelAndView view = new ModelAndView("/food/searchFoodResult");
		
		Integer areaId = null;//根据选择的地区，确定nid
		
		Integer goodClassfyId = RequestUtils.getInt(request, "gid", null);
		FoodInfoQuery query = new FoodInfoQuery();
		query.setState(FoodState.Uped.getValue());
		query.setAreaId(areaId);
		query.setShopId(RequestUtils.getInt(request, "sid", null));
		query.setName(RequestUtils.getString(request, "fname", null)); 
		query.setType(goodClassfyId);
		query.setPagesize(16);
		BasePager<FoodInfo> pagers = this.foodInfoManager.findPage(query);
		
		view.addObject("moreClassfiy", goodsClassifyInfoManager.getById(goodClassfyId));
		List<FoodInfo> moreFoods = pagers.getRows();
		if(null!=moreFoods&&0<moreFoods.size()){
			for(FoodInfo f : moreFoods){
				f.setShopInfo(this.shopInfoManager.getById(f.getId()));
			}
	 		view.addObject("moreFoods", moreFoods);
		}
		
		return view;
	}
}
